package com.aaa.mockwebserver;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okio.ByteString;

public class MockWebServerTest {
    private static final String TAG = "MockWebServerTest";

    public static void main(String[] args) {
        MockWebSocket();
    }

    static class Log {
        static void i(String tag, String msg) {
            System.out.println(tag + " " + msg);
        }
    }

    /**
     * 模拟WebSocket服务器
     */
    public static void MockWebSocket() {
        final MockWebServer mockWebServer = new MockWebServer();

        mockWebServer.enqueue(new MockResponse().withWebSocketUpgrade(new WebSocketListener() {
            @Override
            public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
                super.onClosed(webSocket, code, reason);
                Log.i(TAG, "onClosed: ");
            }

            @Override
            public void onClosing(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
                super.onClosing(webSocket, code, reason);
                Log.i(TAG, "onClosing: ");
            }

            @Override
            public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
                super.onFailure(webSocket, t, response);
                Log.i(TAG, "onFailure: ");
            }

            @Override
            public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
                super.onMessage(webSocket, text);
                Log.i(TAG, "onMessage: ");
            }

            @Override
            public void onMessage(@NotNull WebSocket webSocket, @NotNull ByteString bytes) {
                super.onMessage(webSocket, bytes);
                Log.i(TAG, "onMessage: ");
            }

            @Override
            public void onOpen(@NotNull final WebSocket webSocket, @NotNull Response response) {
                super.onOpen(webSocket, response);
                Log.i(TAG, "onOpen: response=" + response);
                Log.i(TAG, "onOpen: request=" + webSocket.request());

                webSocket.send("您好，这里是服务器，有什么可以为你效劳？");

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(30 * 1000);
                            webSocket.send("30秒后，查看链接是否还在");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }));

        /**
         * 服务器建立后，获取IP地址和端口必须在子线程，否则 NetworkOnMainThreadException
         *
         * 让客户端用获取到的url连接服务器
         * */
        new Thread(new Runnable() {
            @Override
            public void run() {
                String hostName = mockWebServer.getHostName();
                int port = mockWebServer.getPort();
                final String url = "ws:" + hostName + ":" + port;
                Log.i(TAG, "MockWebSocket: url=" + url);
            }
        }).start();

    }
}
